package com.hyk.commonLib.common.utils.dataBinding.bindingAdapter;

import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.BindingAdapter;

import com.hyk.commonLib.common.utils.ScreenUtils;

/**
 * Created by 贺玉琨 on 2020/7/28.
 */
public class MarginAdapter {

    @BindingAdapter("marginHorizontal")
    public static void setHorizontalMargin(View view, float horizontalMargin) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        int horizontalMarginPx = ScreenUtils.dp2px(horizontalMargin);
        layoutParams.setMargins(horizontalMarginPx, layoutParams.topMargin, horizontalMarginPx, layoutParams.bottomMargin);
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("marginVertical")
    public static void setVerticalMargin(View view, float verticalMargin) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        int verticalMarginPx = ScreenUtils.dp2px(verticalMargin);
        layoutParams.setMargins(layoutParams.leftMargin, verticalMarginPx, layoutParams.rightMargin, verticalMarginPx);
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("marginLeft")
    public static void setLeftMargin(View view, float leftMargin) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.setMargins(ScreenUtils.dp2px(leftMargin), layoutParams.topMargin, layoutParams.rightMargin, layoutParams.bottomMargin);
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("marginTop")
    public static void setTopMargin(View view, float topMargin) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.setMargins(layoutParams.leftMargin, ScreenUtils.dp2px(topMargin), layoutParams.rightMargin, layoutParams.bottomMargin);
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("marginRight")
    public static void setRightMargin(View view, float rightMargin) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin, ScreenUtils.dp2px(rightMargin), layoutParams.bottomMargin);
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("marginBottom")
    public static void setBottomMargin(View view, float bottomMargin) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin, layoutParams.rightMargin, ScreenUtils.dp2px(bottomMargin));
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("marginLeftPx")
    public static void setLeftMarginPx(View view, int leftMargin) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.setMargins(leftMargin, layoutParams.topMargin, layoutParams.rightMargin, layoutParams.bottomMargin);
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("marginTopPx")
    public static void setTopMarginPx(View view, int topMargin) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.setMargins(layoutParams.leftMargin, topMargin, layoutParams.rightMargin, layoutParams.bottomMargin);
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("marginRightPx")
    public static void setRightMarginPx(View view, int rightMargin) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin, rightMargin, layoutParams.bottomMargin);
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("marginBottomPx")
    public static void setBottomMarginPx(View view, int bottomMargin) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin, layoutParams.rightMargin, bottomMargin);
        view.setLayoutParams(layoutParams);
    }
}
